var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
var s = require('../../../utils/sign.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo: [],
    img_url: c.img_url
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var data = {
      url: "/yluser/user/gettokenuserinfo",
      token: token,
    };
    utils.request(data).then(function (res) {
      if (res.code == 0) {
        var data = res.data;
        that.setData({
          userinfo: data,
        });
      } else {

      }
    });
  },
  updata: function () {
    var token = wx.getStorageSync("userinfo").wap_token;
    var that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        var data = {
          url: '/comv1/attachment/upimg',
          partner: 5500,
        };
        data['sign'] = s.sign(data);
        wx.uploadFile({
          url: c.api_url,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: data,
          success: function (res) {
            //上传头像
            var data = JSON.parse(res.data);
            var userinfo = that.data.userinfo;
            userinfo['pic'] = data.data.url;
            if (data.code == 0) {
              that.setData({
                userinfo: userinfo
              });
            } else {
              utils.errMsg(data.msg);
            }

            var data = {
              url: "/wsgoods/user/edit",
              token: token,
              ext: 'pic',
              value: data.data.url
            };
            utils.request(data).then(function (res) {
              if (res.code == 0) {
                utils.sucMsg("保存成功");
              } else {
                utils.errMsg(res.msg);
              }
            });

          }
        })
      }
    })
  },
})